<%@page contentType="text/html;charset=UTF-8" import="java.util.*" %>

<%@include file="common.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
  <base href="${ctx}">
  <script src="http://bellard.org/jslinux/term.js"></script>
  <title>${ip}-SHELL</title>
  <style type="text/css">

    .term {
      font-family: courier,fixed,swiss,monospace,sans-serif;
      font-size: 14px;
      color: #f0f0f0;
      background: #000000;
      width: 99%;
      height: 98%;
    }

    .termReverse {
      color: #000000;
      background: #00ff00;
    }
    body{
      padding: 1px;
    }
    table{
      width: 99%;
      height: 98%;
    }
  </style>
</head>

<body>
</body>

<script type="text/javascript">
  var websocket = null;

  //判断当前浏览器是否支持WebSocket
  if('WebSocket' in window){

    if('${ctx}'==''){
      websocket = new WebSocket("ws://"+window.location.host+"/openShell?type=${type}&ip=${ip}");
    }else{
      websocket = new WebSocket("ws://"+window.location.host+"/${ctx}/openShell?type=${type}&ip=${ip}");
    }

  }
  else{
    alert('Not support websocket')
  }

  //连接发生错误的回调方法
  websocket.onerror = function(){
    setMessageInnerHTML("error");
  };

  //连接成功建立的回调方法
  websocket.onopen = function(event){
    setMessageInnerHTML("Welcome...");
  }

  //接收到消息的回调方法
  websocket.onmessage = function(){
    setMessageInnerHTML(event.data);
  }

  //连接关闭的回调方法
  websocket.onclose = function(){
    setMessageInnerHTML("close");
  }

  //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
  window.onbeforeunload = function(){
    websocket.close();
  }

  //将消息显示在网页上
  function setMessageInnerHTML(innerHTML){
    var str  =new String(innerHTML);
    term.write(str);

  }

  //关闭连接
  function closeWebSocket(){
    websocket.close();
  }


  term = new Term(250, 55, term_handler);
  term.open();

  /* send chars to the serial port */
  function term_handler(str)
  {
    websocket.send(str);
  }


</script>
</html>
